import { createRouter, createWebHistory } from 'vue-router';
import Admin from "@/layouts/admin.vue";
import ClassManagement from "@/views/pages/classmanagement/classManagement.vue";
import SpeechBench from "@/views/pages/speechBench.vue";
import ClassDetails from "@/views/pages/classmanagement/classDetails.vue";
import Schedule from "@/components/classDetails/Schedule.vue";
import Completed from "@/components/classDetails/Completed.vue";
import Attendance from "@/components/classDetails/Attendance.vue";
import Students from "@/components/classDetails/Students.vue";
import Class from "@/views/pages/class.vue"
import UserManagement from "@/views/pages/systemmanagement/UserManagement.vue";
import RoleManagement from "@/views/pages/systemmanagement/RoleManagement.vue";
import MenuManagement from "@/views/pages/systemmanagement/MenuManagement.vue";

const asyncRoutes = [
  {
    path: '/educational/classManagement/class',
    name: '/educational/classManagement/class',
    component: ClassManagement,
    meta: {
      title: '班级管理'
    },
  },
  {
    path:'/educational/class',
    component: Class,
    meta: {
      title: '课次管理'
    }
  },
  {
    path: '/educational/speechbench',
    name: '/educational/speechBench',
    component: SpeechBench,
    meta: {
      title: '讲台'
    },
  },
  {
    path: '/educational/classManagement/classDetails/:id',
    name: 'classDetails',
    component: ClassDetails,
    meta: {
      title: '班级详情页',
      hidden: true
    },
    children: [
      {
        path: 'schedule',
        name: 'classDetailsSchedule',
        component: Schedule,
        meta: {
          title: '排课表'
        }
      },
      {
        path: 'completed',
        name: 'classDetailsCompleted',
        component: Completed,
        meta: {
          title: '已上课表'
        }
      },
      {
        path: 'attendance',
        name: 'classDetailsAttendance',
        component: Attendance,
        meta: {
          title: '签到记录'
        }
      },
      {
        path: 'students',
        name: 'classDetailsStudents',
        component: Students,
        meta: {
          title: '班级学员'
        }
      },
      {
        path: '',
        redirect: 'schedule'
      }
    ]
  },
  {
    path:'/educational/system/userManagement',
    name: '/educational/system/userManagement',
    component: UserManagement,
    meta: {
      title: '用户管理'
    }
  },{
    path:'/educational/system/roleManagement',
    name: '/educational/system/roleManagement',
    component: RoleManagement,
    meta: {
      title:'角色管理'
    }
  },{
    path:'/educational/system/menuManagement',
    name: '/educational/system/menuManagement',
    component: MenuManagement,
    meta: {
      title: '菜单管理'
    }
  },


];

export const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'admin',
      component: Admin,
      meta: {
        title: 'Home',
      },
      children: asyncRoutes // 将 asyncRoutes 直接添加到 admin 子路由
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/pages/login.vue'),
      meta: {
        title: '登录页',
      }
    },
    {
      path: '/home',
      name: 'homepage',
      component: () => import('../views/home/home.vue'),
      meta: {
        title: '后台首页',
      }
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      component: () => import('../views/pages/404.vue'),
    },
  ],
});

// 动态添加路由的方法（保留以支持其他动态路由）
export function addRoutes(menus) {
  let hasNewRoutes = false;
  const findAndRoutesByMenus = (arr) => {
    arr.forEach(e => {
      let item = asyncRoutes.find(o =>
          e.path === o.path ||
          e.path.replace(/\/$/, '') === o.path.replace(/:.*$/, '').replace(/\/$/, '')
      );

      if (item && !router.hasRoute(item.name)) {
        router.addRoute("admin", item);
        hasNewRoutes = true;
        console.log('成功添加路由:', item.path);
      }

      if (e.childrenMenus && e.childrenMenus.length > 0) {
        findAndRoutesByMenus(e.childrenMenus);
      }
    });
  };
  findAndRoutesByMenus(menus);
  console.log('最终路由表:', router.getRoutes());
  return hasNewRoutes;
}